<?php
class prestamo{
  public $capital;
  public $cuota;
  public $interes;
  public $interesacum;
  public $anios;
  public $npagos;
  public $fecha;
  
  public $dia;
  public $mes;
  public $anio;
  
  var $arrayprestamo = array();
  
  public function rellenarvariables(){
    $this->mes = (int) $_REQUEST["mes"];
    $this->dia = (int) $_REQUEST["dia"];
    $this->anio = (int) $_REQUEST["anio"];
    $this->npagos = (int) $_REQUEST["npagos"];    
    $this->fecha = $this->comprobarfecha();
    $this->capital = (float) $_REQUEST['capital'];
    $this->anios = (int) $_REQUEST["anios"];
    $this->interes = (float) $_REQUEST["interes"]/100/$this->npagos;
    $potencia = pow((1+$this->interes),($this->anios *$this->npagos));
    $this->cuota = $this->capital*(($this->interes*$potencia)/($potencia - 1));
  }
  public function rellenarmatriz(){
    $contador = 0;
    $interesacum = 0;
    $capitalantes = $this->capital;
    $capitalpendiente = $this->capital;
//number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
    while($capitalpendiente > 0){
      $fecha = $this->comprobarfecha();
      $capitalanterior = $capitalantes;
      if($capitalpendiente < $this->cuota){
        $cuota = $capitalpendiente;
        $intereses = ($capitalantes * $this->interes);
        $principal = $capitalpendiente;
      }else{
        $cuota = $this->cuota;
        $intereses = ($capitalantes * $this->interes);
        $principal = $this->cuota - ($capitalantes * $this->interes);
      }
      $this->interesacum = $this->interesacum + ($capitalantes * $this->interes);
      $capitalpendiente = $capitalantes - $principal;
      $pendiente = $capitalpendiente;
      $this->arrayprestamo[$contador]=Array("fecha"=>$fecha,
                                            "capitalanterior"=>$capitalanterior,
                                            "cuota"=>$cuota,
                                            "intereses"=>$intereses,
                                            "principal"=>$principal,
                                            "pendiente"=>$pendiente);
      $capitalantes = $capitalpendiente;
      $contador++;
      $this->mes = $this->mes + (12/$this->npagos);
    }
  }
  public function visualizarmatriz(){
    CabecerasCuadro();
    foreach ($this->arrayprestamo as $indice => $elemento) {
      echo "<tr>";
      echo "<td>".($indice + 1)."</td>";
      echo "<td>".$elemento['fecha']."</td>";
      echo "<td>".number_format(($elemento['capitalanterior']),2,',','.')."</td>";
      echo "<td>".number_format(($elemento['cuota']),2,',','.')."</td>";
      echo "<td>".number_format(($elemento['intereses']),2,',','.')."</td>";
      echo "<td>".number_format(($elemento['principal']),2,',','.')."</td>";
      echo "<td>".number_format(($elemento['pendiente']),2,',','.')."</td>";
      echo "</td></tr>";      
    }
    echo "</table></center>";
  }
  public function comprobarfecha(){
    //checkdate(mes,dia,año)
    if(checkdate($this->mes, $this->dia, $this->anio)){
      return(date('d-m-Y', mktime(0,0,0,$this->mes,$this->dia,$this->anio)));
    }
    else{
      if($this->mes > 12){
        $this->mes = $this->mes - 12;
        $this->anio++;
        return(date('d-m-Y', mktime(0,0,0,$this->mes,$this->dia,$this->anio)));
      }
      elseif(checkdate ($this->mes, ($this->dia - 1), $this->anio)){
        return(date('d-m-Y', mktime(0,0,0,$this->mes,($this->dia - 1),$this->anio)));
      }
      elseif(checkdate ($this->mes, ($this->dia - 2), $this->anio)){
        return(date('d-m-Y', mktime(0,0,0,$this->mes,($this->dia - 2),$this->anio)));
      }
      elseif(checkdate ($this->mes, ($this->dia - 3), $this->anio)){
        return(date('d-m-Y', mktime(0,0,0,$this->mes,($this->dia - 3),$this->anio)));
      }
    }
  }
  public function resumen(){
    echo "<br><br><center><table><tr>";
    echo "<th>Capital</th>";
    echo "<th>Interes</th>";
    echo "<th>Años</th>";
    echo "<th>Pagos por año</th>";
    echo "<th>Fecha de inicio</th><tr>";
    
    echo "<tr><td>".number_format($this->capital,2,',','.')."</td>";
    echo "<td>".number_format(($this->interes*100*$this->npagos),2,',','.')." %</td>";
    echo "<td>$this->anios</td>";
    echo "<td>$this->npagos</td>";
    echo "<td>$this->fecha</td>";
    echo "</tr></table></center>";
    
    echo "<center><table><tr>";
    echo "<tr><th>Cuota</th>";
    echo "<th>Intereses</th>";
    echo "<th>Capital total</th></tr>";
    echo "<tr><td>".number_format($this->cuota,2,',','.')."</td>";
    echo "<td>".number_format($this->interesacum,2,',','.')."</td>";
    echo "<td>".number_format(($this->capital + $this->interesacum),2,',','.')."</td>";
    echo "</tr></table>";
    echo "</center>";
  }
  public function imprimirpdf(){
    //Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]])
    require('fpdf.php');
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',10);
    $pdf->SetXY(32, 10); //margen de 42
    //Colores para los encabezados ====================================
    $pdf->SetFillColor(0,0,0);
    $pdf->SetTextColor(255,255,255);
    $pdf->SetDrawColor(255,255,255);
    $pdf->SetLineWidth(.1);
    $pdf->Cell(18,6,"Captital",1,0,'C',true);
    $pdf->Cell(18,6,(utf8_decode("Interés")),1,0,'C',true);
    $pdf->Cell(11,6,(utf8_decode("Años")),1,0,'C',true);
    $pdf->Cell(21,6,(utf8_decode("Cuotas/año")),1,0,'C',true);
    $pdf->Cell(23,6,"Fecha inicio",1,0,'C',true);
    $pdf->Cell(15,6,"Cuota",1,0,'C',true);
    $pdf->Cell(18,6,"Intereses",1,0,'C',true);
    $pdf->Cell(23,6,"Capital total",1,1,'C',true);    
    //Resumen prestamo ================================================
    $pdf->SetFillColor(255,255,255);
    $pdf->SetTextColor(0,0,0);
    $pdf->SetX(32); //margen de 42
    $pdf->Cell(18,6,(number_format($this->capital,2,',','.')),0,0,'C');
    $pdf->Cell(18,6,(number_format(($this->interes*100*12),2,',','.')),0,0,'C');
    $pdf->Cell(11,6,($this->anios),0,0,'C');
    $pdf->Cell(21,6,($this->npagos),0,0,'C');
    $pdf->Cell(23,6,$this->fecha,0,0,'C');
    $pdf->Cell(15,6,(number_format($this->cuota,2,',','.')),0,0,'C');
    $pdf->Cell(18,6,(number_format($this->interesacum,2,',','.')),0,0,'C');
    $pdf->Cell(23,6,(number_format(($this->capital + $this->interesacum),2,',','.')),0,1,'C'); 
    ////Colores para los encabezados ====================================
    $pdf->SetFillColor(0,0,0);
    $pdf->SetTextColor(255,255,255);
    $pdf->SetDrawColor(255,255,255);
    //Cabeceras de tabla ==============================================
    $pdf->SetXY(25,($pdf->GetY() + 5)); //nos posicionamos a 25 del límete de la hoja
    $pdf->Cell(20,6,(utf8_decode("Nºde pago")),1,0,'C', true);
    $pdf->Cell(20,6,"fecha",1,0,'C',true);
    $pdf->Cell(42,6,"Capital antes de pago",1,0,'C',true);
    $pdf->Cell(12,6,"Cuota",1,0,'C',true);
    $pdf->Cell(18,6,"Intereses",1,0,'C',true);
    $pdf->Cell(18,6,"Principal",1,0,'C',true);
    $pdf->Cell(33,6,"Capital pendiente",1,1,'C',true);
    $pdf->SetY($pdf->GetY() + 1);
    $pdf->SetFont('Arial','',8);
    //Reestablecemos los colores ======================================
    $pdf->SetFillColor(255,255,255);
    $pdf->SetTextColor(0,0,0);
    $pdf->SetDrawColor(0,0,0);
    foreach ($this->arrayprestamo as $indice => $elemento) {
      $pdf->SetX(25);
      $pdf->Cell(20,4,($indice + 1),1,0,'R');
      $pdf->Cell(20,4,$elemento['fecha'],1,0,'C');
      $pdf->Cell(42,4,(number_format(($elemento['capitalanterior']),2,',','.')),1,0,'R');
      $pdf->Cell(12,4,number_format(($elemento['cuota']),2,',','.'),1,0,'R');
      $pdf->Cell(18,4,number_format(($elemento['intereses']),2,',','.'),1,0,'R');
      $pdf->Cell(18,4,number_format(($elemento['principal']),2,',','.'),1,0,'R');
      $pdf->Cell(33,4,number_format(($elemento['pendiente']),2,',','.'),1,1,'R'); 
    }
    //$pdf->Output('prestamo.pdf','D');
    $pdf->Output();
  }
}
?>